package com.lottery.ruok.lottrey.chat.model.system;

import android.annotation.SuppressLint;
import android.os.Parcel;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

/**
 * 作者：lm
 * <p>
 * 创建日期：2018/9/12$
 * <p>
 * 描述：系统消息，主要是管理员的变更
 */
@MessageTag(value = "RCD:SysMsg", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class SystemMessage extends MessageContent {
    /*#define RCDSystemMessageTypeIdentifier @“RCD:SysMsg”
    系统消息
    msgId    系统消息id long
    content   消息内容（如“XXX被提升到了管理员“）
    extra    备注 string
    targetUserId 被操作的目标用户id string
    status 被操作的目标用户操作状态 0: 其它，1:加为管理员； 2:移除管理员 int
    type. 1:管理员操作。2:普通会员操作（踢出禁言等） 3:普通会员进入离开聊天室 4:普通会员进入离开聊天室 int
    targetId 房间Id string
    date 时间 string*/
    private static final String TAG = "SystemMessage";
    private static final String USER = "user";
    public static final String MSG_ID = "msgId";
    public static final String CONTENT = "content";
    public static final String RXTRA = "extra";
    public static final String TARGET_USER_ID = "targetUserId";
    public static final String STATUE = "status";
    public static final String TYPE = "type";
    public static final String TARGET_ID = "targetId";
    public static final String DATE = "date";

    private long msgId;
    private String content;
    private String extra;
    private String targetUserId;
    private int status;
    private int type;
    private String targetId;
    private String date;

    public SystemMessage() {

    }

    public SystemMessage(long msgId,
                         String content,
                         String extra,
                         String targetUserId,
                         int status,
                         int type,
                         String targetId,
                         String date) {
        this.msgId=msgId;
        this.content=content;
        this.extra=extra;
        this.targetUserId=targetUserId;
        this.status=status;
        this.type=type;
        this.targetId=targetId;
        this.date=date;

    }

    public static final Creator<SystemMessage> CREATOR = new Creator<SystemMessage>() {
        @Override
        public SystemMessage createFromParcel(Parcel source) {
            return new SystemMessage(source);
        }

        @Override
        public SystemMessage[] newArray(int size) {
            return new SystemMessage[size];
        }
    };



    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put(MSG_ID, getMsgId()); // 这里的id（联系人）不同于下边发送名片信息者的 sendUserId
            jsonObject.put(CONTENT, getContent());
            jsonObject.put(RXTRA, getExtra());
            jsonObject.put(TARGET_USER_ID, getTargetUserId());
            jsonObject.put(STATUE, getStatus());
            jsonObject.put(TYPE, getType());
            jsonObject.put(TARGET_ID, getTargetId());
            jsonObject.put(DATE, getDate());
            if (getJSONUserInfo() != null)
                jsonObject.putOpt(USER, getJSONUserInfo());
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

        try {
            return jsonObject.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
public SystemMessage(byte[] data){
    String jsonStr = null;
    try {
        jsonStr = new String(data, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        JSONObject jsonObj = new JSONObject(jsonStr);

        if (jsonObj.has(MSG_ID))
            setMsgId(jsonObj.optLong(MSG_ID));
        if (jsonObj.has(CONTENT))
            setContent(jsonObj.optString(CONTENT));

        if (jsonObj.has(RXTRA))
            setExtra(jsonObj.optString(RXTRA));
        if (jsonObj.has(TARGET_USER_ID))
            setTargetUserId(jsonObj.optString(TARGET_USER_ID));
        if (jsonObj.has(STATUE))
            setStatus(jsonObj.optInt(STATUE));


        if (jsonObj.has(TYPE))
            setType(jsonObj.optInt(TYPE));
        if (jsonObj.has(TARGET_ID))
            setTargetId(jsonObj.optString(TARGET_ID));
        if (jsonObj.has(DATE)) {
            setDate(jsonObj.optString(DATE));
        }
        if (jsonObj.has(USER))
            setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user")));
    } catch (JSONException e) {
        RLog.e(TAG, "JSONException " + e.getMessage());
    }
}


    public SystemMessage(Parcel in) {

        msgId = in.readLong();
        content = in.readString();
        extra = in.readString();
        targetUserId = in.readString();
        status = in.readInt();
        type = in.readInt();
        targetId = in.readString();
        date = in.readString();

        setUserInfo(ParcelUtils.readFromParcel(in, UserInfo.class));

    }
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(msgId);
        dest.writeString(content);
        dest.writeString(extra);
        dest.writeString(targetUserId);
        dest.writeInt(status);
        dest.writeInt(type);
        dest.writeString(targetId);
        dest.writeString(date);
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    public long getMsgId() {
        return msgId;
    }

    public void setMsgId(long msgId) {
        this.msgId = msgId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public String getTargetUserId() {
        return targetUserId;
    }

    public void setTargetUserId(String targetUserId) {
        this.targetUserId = targetUserId;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}
